package com.base.components.common.util;

import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;

import java.util.Collections;
import java.util.Set;

/**
 * SetsHelper
 *
 * @author <a href="drakelee1221@gmail.com">LiGeng</a>
 * @version v1.0.0
 * @date 2018-07-06 14:07
 */
public abstract class SetsHelper {

  /**
   * 比较两个Set中是否都含有同一个元素
   * @param s1
   * @param s2
   *
   * @return
   */
  public static <E> boolean existsSameElement(Set<E> s1, Set<E> s2){
    return CollectionHelper.existsSameElement(s1, s2);
  }

  /**
   * 把字符串分割后，转换为Set
   * @param srcString -
   * @param separatorChars -
   * @return Set
   */
  public static Set<String> toStringSets(String srcString, String separatorChars) {
    Set<String> tags = Collections.emptySet();
    if (StringUtils.isNotBlank(srcString)) {
      tags = Sets.newHashSet(StringUtils.split(srcString, separatorChars));
    }
    return tags;
  }

  /**
   * 把字符串分割后，转换为Set
   * @param srcString -
   * @param separatorChars -
   * @param typeClass -
   * @return Set
   */
  @SuppressWarnings("unchecked")
  public static <E> Set<E> toTypeSets(String srcString, String separatorChars, Class<E> typeClass) {
    if (StringUtils.isNotBlank(srcString)) {
      String[] split = StringUtils.split(srcString, separatorChars);
      if(split != null && split.length > 0){
        Set<E> set = Sets.newHashSet();
        for (String s : split) {
          if(String.class.equals(typeClass)){
            set.add((E) s);
          }else{
            set.add(ConvertUtil.convert(s, typeClass));
          }
        }
        return set;
      }
    }
    return Collections.emptySet();
  }

}
